package com.internal.Login.server;

import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.internal.Internalhibernate.client.SecUsers;
import com.internal.Internalhibernate.server.dao.SecUsersDAO;
import com.internal.Login.client.LoginService;
import com.internal.SharedModule.client.SecUsersDTO;
import com.internal.SharedModule.server.CloneServerUtil;

public class LoginServiceImpl extends RemoteServiceServlet implements
		LoginService {

	@Override
	public String login(String userId, String pass) throws Exception {
		SecUsers user = new SecUsersDAO().getUserByAuthInfo(userId, pass);
		if (user == null) {
			return "fail";
		} else if (!user.isActiveFlag()) {
			return "noactive";
		}
		SecUsersDTO clonedUser = CloneServerUtil.cloneSecUser(user);
		getOpenedSession(false).setAttribute("user", clonedUser);
		getOpenedSession(false).setAttribute("userHiber", user);

		return "success";
	}

	public HttpSession getOpenedSession(final boolean isOpenedSession) {
		try {
			HttpSession session = null;
			if (this.getThreadLocalRequest().getSession(isOpenedSession) != null) {
				session = this.getThreadLocalRequest().getSession(
						isOpenedSession);
			} else {
				session = this.getThreadLocalRequest().getSession();
			}
			return session;
		} catch (final Exception e) {
			return null;
		}
	}

	@Override
	public void sendPassword(String userId) throws Exception {
		SecUsers user = new SecUsersDAO().isEmailExist(userId);
		// add MailSSL in shared module
//		if(user != null)
//		SendMailSSL.send(user.getUserPassword(), userId);

	}

}
